import Vue from 'vue'
import VueRouter from 'vue-router'
// 目标1：通过路由实现登录注册组件的切换
// 1.1 创建两个组件：views/Login/Login   views/Reg/Reg.vue
// 1.2 在router/index.js 中进行路由配置
// 1.3 在App.vue 中使用 router-view 组件占位

//  在 js 代码中写的@符号 都会被解析成 src 的绝对路径
// import Login from '@/views/Login/Login.vue'
// const login = ()=>import('@/views/Login/Login.vue')
// import Reg from '@/views/Reg/Reg.vue'
// import Main from '@/views/Main/Main.vue'
import store from '@/store'
// import Home from '@/views/Menus/Home/Home.vue'
// import UserInfo from '@/views/Menus/User/UserInfo.vue'
// import UserAvatar from '@/views/Menus/User/UserAvatar.vue'
// import UserPwd from '@/views/Menus/User/UserPwd.vue'
// import ArtCate from '@/views/Menus/ArtCate/ArtCate.vue'
// import ArtList from '@/views/Menus/ArtCate/ArtList.vue'
Vue.use(VueRouter)

const routes = [
  //  一个对象表示一条路由规则
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/Login.vue')
  },
  { path: '/reg', name: 'Reg', component: () => import('@/views/Reg/Reg.vue') },
  {
    path: '/',
    name: 'Main',
    component: () => import('@/views/Main/Main.vue'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Menus/Home/Home.vue')
      },
      {
        path: 'user-info',
        name: 'UserInfo',
        component: () => import('@/views/Menus/User/UserInfo.vue')
      },
      {
        path: 'user-avatar',
        name: 'UserAvatar',
        component: () => import('@/views/Menus/User/UserAvatar.vue')
      },
      {
        path: 'user-pwd',
        name: 'UserPwd',
        component: () => import('@/views/Menus/User/UserPwd.vue')
      },
      {
        path: 'art-cate',
        name: 'ArtCate',
        component: () => import('@/views/Menus/ArtCate/ArtCate.vue')
      },
      {
        path: 'art-list',
        name: 'ArtList',
        component: () => import('@/views/Menus/ArtCate/ArtList.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由的全局前置导航守卫：路由切换之前进行拦截

router.beforeEach((to, from, next) => {
  // 当用户访问 / 路径时，并且当前未登录，就要强行跳转到/login
  if (to.path === '/' && !store.state.token) {
    Vue.prototype.$message.error('请登录')
    return next('/login')
  }
  next()
})
export default router
